#!/usr/bin/env bash
if ! type mvn > /dev/null 2>&1
then
    echo maven is required. Aborting...
    exit 1
fi

if ! type dos2unix > /dev/null 2>&1; then
    echo dos2unix is required. Aborting...
    exit 1
fi

appName=openapi3-doc
version=1.0.0      # 必须和pom.xml的<version></version>版本号一致!!!
envs="test prod"
curPath=$(pwd)
targetPath=${curPath}/target
buildPath=${curPath}/build
packagePath=${buildPath}/${appName}
commitId=$(git rev-parse --short HEAD)

if [[ "$#"  -eq "1" ]]; then
    envs="$1"
fi

rm -rf ${buildPath}
mkdir -p ${buildPath}

for env in ${envs}
do
    rm -rf ${packagePath} 
    mkdir -p ${packagePath}
    mvn clean 
    mvn package -P ${env}

    README=${packagePath}/README.md
    echo 项目: ${appName} > ${README}
    echo 项目版本: ${version}.${commitId} >> ${README}
    echo GIT: $(git rev-parse HEAD) >> ${README}
    echo 打包日期: $(date) >> ${README}

    dos2unix conf/install.sh
    chmod +x conf/install.sh

    cp ${targetPath}/${appName}-${version}.jar ${packagePath}/${appName}.jar
    cp conf/install.sh ${packagePath}
    cp conf/${appName}-${env}.ini ${packagePath}/${appName}.ini
    
    cd $buildPath
    zip -r ${appName}-${version}-${env}.${commitId}.zip ${appName}
    cd $curPath
done

rm -rf ${packagePath}
mvn clean
